package proj2501.image_viewer;

import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JFrame;


public class P013ImageViewer extends JFrame {
	private ImagePanel imagePanel;
	
	public P013ImageViewer() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(800, 600);
		setTitle("Image Viewer");
		setJMenuBar(new IVMenuBar(this));
		imagePanel =new ImagePanel(); 
		add(imagePanel);
		
		open("test01.jpg");
		
		setVisible(true);
	}
	
	public void open(String path) {
		imagePanel.open(path);
	}
	
	public OpenDialogAction openDialogAction = new OpenDialogAction();
	private class OpenDialogAction extends AbstractAction {
		public OpenDialogAction() {
			super("Open...");
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			JFileChooser chooser = new JFileChooser();
			int result = chooser.showOpenDialog(P013ImageViewer.this);
			if (result == JFileChooser.APPROVE_OPTION) {
				File file = chooser.getSelectedFile();
				open(file.getPath());
			}
		}
	}
	
	public CloseAction closeAction = new CloseAction();
	private class CloseAction extends AbstractAction {
		public CloseAction() {
			super("Quit Program");
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			P013ImageViewer frame = P013ImageViewer.this;
			frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
		}
	}
}
